﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Icst.Leaflike.Logging;

namespace Icst.Leaflike.Data
{
    public class Book : DataContext<Book>
    {
        protected static readonly string CACHE_ALL = "/Book/All";

        public sys_book[] Top(int count)
        {
            return All.Take(count).ToArray();
        }
        public sys_book[] get(int page, int pagesize, out int total)
        {
            sys_book[] all = All;
            total = all.Count();
            return all.Skip((page - 1) * pagesize).Take(pagesize).ToArray();
        }
        public sys_book[] All
        {
            get
            {
                try
                {
                    return CachePolicy.GetIf<sys_book[]>(
                        CACHE_ALL,
                        () => { return Context.sys_books.OrderByDescending(p => p.pub_date).ToArray(); });
                }
                catch (Exception e)
                {
                    Logger.Error("FAILED TO get books." + e.Message);
                    return new sys_book[] { };
                }
            }
        }
        public override void Refresh()
        {
            base.Refresh();

            Logger.Info("REFRESH CachePolicy.Clear");
            try { CachePolicy.Delete(CACHE_ALL); }
            catch { }
        }
    }
}
